package net.sk.china.city.controller;

import net.sk.china.city.domain.dto.user.UserItemDTO;
import net.sk.china.city.domain.dto.user.UserSearchDTO;
import net.sk.china.city.domain.dto.user.UserUpdateDTO;
import net.sk.china.city.service.IUserService;
import net.sk.china.city.controller.base.BaseController;
import net.sk.china.city.domain.dto.AjaxResult;
import net.sk.china.city.domain.dto.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 前台用户Controller
 *
 * @author yangguanjin
 * @date 2024-04-07
 */
@Api(tags = "前台用户（志愿者）管理")
@RequiredArgsConstructor
@RestController("cityUserController")
@RequestMapping("/api/user")
public class UserController extends BaseController {
    @Qualifier("cityUserService")
    private final IUserService userService;


    /**
     * 查询前台用户列表
     */
    @ApiOperation("查询用户页")
    @PreAuthorize("hasAnyAuthority('user:management:list')")
    @PostMapping("/list")
    public TableDataInfo<UserItemDTO> list(@RequestBody UserSearchDTO userSearchDTO) {
        startPage();
        return userService.selectUserList(userSearchDTO);
    }

    @ApiOperation("查询用户页2")
    @PreAuthorize("hasAnyAuthority('user:management:list')")
    @PostMapping("/list2")
    public TableDataInfo<UserItemDTO> list2(@RequestBody UserSearchDTO userSearchDTO) {
        startPage();
        return userService.selectUserList2(userSearchDTO);
    }

    /**
     * 导出前台用户列表
     */
    @ApiOperation(value = "excel导出用户")
    @PreAuthorize("hasAnyAuthority('user:management:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody UserSearchDTO userDTO) {
        TableDataInfo<UserItemDTO> tableDataInfo = userService.selectUserList(userDTO);
        List<UserItemDTO> list = tableDataInfo.getRows();
//        ExcelUtil<UserItemDTO> util = new ExcelUtil<>(UserItemDTO.class);
//        util.exportExcel(response, list, "前台用户数据");
    }

    @ApiOperation(value = "excel增量导入用户（志愿者）")
    @PostMapping("/import")
    public AjaxResult importUser(@RequestPart MultipartFile multipartFile) {
        return success(userService.importUser(multipartFile));
    }

    @ApiOperation(value = "excel全量导入用户（志愿者）")
    @PostMapping("/fullImport")
    public AjaxResult fullImportUser(@RequestPart MultipartFile multipartFile) {
        return success(userService.fullImportUser(multipartFile));
    }

    /**
     * 获取前台用户详细信息
     */
    @ApiOperation("查询用户详情")
    @PreAuthorize("hasAnyAuthority('user:management:query')")
    @GetMapping(value = "/{pkId}")
    public AjaxResult getInfo(@PathVariable("pkId") Long pkId) {
        return success(userService.selectUserByPkId(pkId));
    }

    // todo 维护网格表的志愿者数量

    /**
     * 新增前台用户
     */
    @ApiOperation("增加用户")
    @PreAuthorize("hasAnyAuthority('user:management:add')")
    @PostMapping
    public AjaxResult add(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toAjax(userService.insertUser(userUpdateDTO));
    }

    /**
     * 修改前台用户
     */
    @ApiOperation("修改用户")
    @PreAuthorize("hasAnyAuthority('user:management:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toAjax(userService.updateUser(userUpdateDTO));
    }

    /**
     * 修改积分
     */
    @ApiOperation("修改积分")
    @PreAuthorize("hasAnyAuthority('user:management:point')")
    @PutMapping("/point")
    public AjaxResult editPoint(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toAjax(userService.updatePoint(userUpdateDTO));
    }

    /**
     * 删除前台用户
     */
    @ApiOperation("删除用户")
    @PreAuthorize("hasAnyAuthority('user:management:remove')")
    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        return toAjax(userService.deleteUserByPkIds(pkIds));
    }
}
